<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="设备名称" prop="equipName">
        <el-input
          v-model="queryParams.equipName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备SN号" prop="snNum">
        <el-input
          v-model="queryParams.snNum"
          placeholder="请输入设备SN号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="行政区" prop="region">
        <el-input
          v-model="queryParams.region"
          placeholder="请输入行政区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
            <el-form-item label="请选择日期" prop="createTime">
              <div class="">
                <el-date-picker
                  :clearable="false"
                  v-model="createTime"
                  type="daterange"
                  align="right"
                  style="width: 240px"
                  value-format="yyyy-MM-dd"
                  unlink-panels
                  range-separator="-"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  @change="dateFormat"
                  >
                </el-date-picker>
              </div>
            </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:analyse:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="analyseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" type="index"/>
      <el-table-column label="设备名称" align="center" prop="equipName"/>
      <el-table-column label="sn序号" align="center" prop="snNum"/>
      <el-table-column label="行政区域" align="center" prop="region"/>
      <el-table-column label="详细地址" align="center" prop="address"/>
      <el-table-column label="触发器名称" align="center" prop="trigerName"/>
      <el-table-column label="报警次数" align="center" prop="warningNum"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-model="analyseList.snNum"
            size="mini"
            type="text"
            icon="el-icon-location-outline"
            @click="handleUpdate(scope.row.snNum)"
          >定位
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="viewDetails(scope.row)"
            v-hasPermi="['system:alarm:edit']"
          >查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :title="title" :visible.sync="open" width="75%" custom-class="import-dialog" style="margin: auto" append-to-body>
      <el-row>
        <el-col>
          <span class="map">地图位置</span>
        </el-col>
        <el-col>
          <map-component :lat-anglng="latAndLng"></map-component>
        </el-col>
      </el-row>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="opens" width="75%" custom-class="import-dialog" style="margin: auto" append-to-body>
      <span>设备{{this.form.snNum}}详情</span>
      <br>
      <span>{{this.form.trigerName}}（{{this.form.startTime}}~{{this.form.endTime}}）</span>
      <el-row>
        <div id="main" style="width: 95%;height: 360px;"></div>
      </el-row>
    </el-dialog>

  </div>
</template>

<script>
import {listAnalyse, listmap, listNum} from "@/api/system/analyse";
import {pcaTextArr} from "element-china-area-data";
import MapComponent from '../../components/MapComponent.vue';


export default {
  components:{
    'map-component': MapComponent
  },
  name: "Analyse",
  dicts: ['ownership_unit'],
  data() {
    let nowDate=new Date();
    let date={
      year:nowDate.getFullYear(),
      month:nowDate.getMonth()+1,
      date:nowDate.getDate(),
    };
    //当前日期
    let  sysDate=date.year+"-"+date.month+"-"+date.date;
    var beforeDate=new Date(nowDate);
    beforeDate.setDate(nowDate.getDate()-30);
    //30天 前
    let systemDateBegin = `${beforeDate.getFullYear()}-${beforeDate.getMonth() + 1<10?`0${beforeDate.getMonth() + 1}`:beforeDate.getMonth() + 1}-${beforeDate.getDate()}`;

    return {
      options: pcaTextArr,
      dialogTableVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      analyseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      opens: false,
      //地图定位
      latAndLng:{lat:1,lng:1},
      //定位
      mapList:{},

      createTime: [systemDateBegin,sysDate],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipName: null,
        snNum: null,
        regionId: null,
        region: null,
        address: null,
        trigerId: null,
        trigerName: null,
        warningNum: null,
        startTime: systemDateBegin,
        endTime: sysDate,
        lat:null,
        lng: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 报警值
      cautionNum:[],
      times:[],
      num:[],
    };
  },

  created() {
    this.getList();
  },

  mounted() {
  },

  methods: {
    //获取时间
    dateFormat(picker){
      console.log(picker)
      this.queryParams.startTime = picker[0]
      this.queryParams.endTime = picker[1]
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      this.queryParams.createTime = null;
      listAnalyse(this.queryParams).then(response => {
        this.analyseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        equipName: null,
        snNum: null,
        regionId: null,
        region: null,
        address: null,
        trigerId: null,
        trigerName: null,
        warningNum: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleChange(res) {
      this.form.address = res
    },

    /** 定位按钮操作 */
    handleUpdate(row) {
      //设置弹窗
      this.dialogVisible =true
      listmap(row).then(response => {
        //是否显示弹窗
        this.open = true;
        this.mapList = response.rows;
        this.latAndLng.lat= Object.assign({},this.mapList[0]).lat;
        this.latAndLng.lng= Object.assign({},this.mapList[0]).lng;
      });
    },


    /** 查看详情按钮操作 */
    viewDetails(row) {
      console.log(row)
      row.startTime = this.queryParams.startTime;
      row.endTime = this.queryParams.endTime;
      listNum(row).then(response => {
        this.cautionNum = response.rows;
        this.form.startTime=this.queryParams.startTime
        this.form.endTime=this.queryParams.endTime
        this.form.trigerName=row.trigerName
        console.log("1",this.form.trigerName)
        this.times = this.cautionNum.map(res => new Date(res.createTime).toLocaleDateString());
        this.num = this.cautionNum.map(res => res.warningValue);
        this.opens = true;
        this.$nextTick(() => {
          this.myEcharts();
        });
      });

    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/analyse/export', {
        ...this.queryParams
      }, `analyse_${new Date().getTime()}.xlsx`)
    },
    myEcharts(){
      var myChart = this.$echarts.init(document.getElementById('main'));
      //配置图表
      var option = {
        xAxis: {
          type: 'category',
          data: this.times
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: this.num,
            type: 'line'
          }
        ]
      };
      myChart.setOption(option);
    }

  }
};

</script>

<style>
.plat1{
  min-height: 30px;
}
.map{
  display: inline-block;
  height: 70%;
}
.container-box .el-dialog.import-dialog{
    height: 75%;
    max-height: 80vh;
    overflow-y: auto;
  }
.container-box .el-dialog__body{
    max-height: 70vh!important;
  }
.map1{
  display: inline-block;
  height: 70%;
  width: 70%;
}

</style>
